import request from '@/utils/request'

// 查询业务订单信息列表
export function listBusinessOrder(query) {
  return request({
    url: '/ruoyi/businessOrder/list',
    method: 'get',
    params: query
  })
}

// 查询业务订单信息详细
export function getBusinessOrder(orderId) {
  return request({
    url: '/ruoyi/businessOrder/' + orderId,
    method: 'get'
  })
}

// 根据员工ID获取订单列表
// export function listBusinessOrderByEmployee(employeeId) {
//   return request({
//     url: `/ruoyi/businessOrder/employee/${employeeId}`,
//     method: 'get'
//   })
// }

// 根据员工ID和支付状态获取订单列表 原名：listBusinessOrderByEmployee
export function getListByEmployeeIdAndPayStatus(employeeId, payStatus = null) {
  return request({
    url: `/ruoyi/businessOrder/employee/payStatus/${employeeId}`,
    method: 'get',
    params: {
      payStatus: payStatus // 添加 payStatus 查询参数
    }
  })
}

// 根据员工ID和订单状态获取订单列表
export function getListByEmployeeIdAndOrderStatus(employeeId, orderStatus = null) {
  return request({
    url: `/ruoyi/businessOrder/employee/orderStatus/${employeeId}`,
    method: 'get',
    params: {
      orderStatus: orderStatus // 添加 orderStatus 查询参数
    }
  })
}

// 新增业务订单信息
export function addBusinessOrder(data) {
  return request({
    url: '/ruoyi/businessOrder',
    method: 'post',
    data: data
  })
}

// 修改业务订单信息
export function updateBusinessOrder(data) {
  return request({
    url: '/ruoyi/businessOrder',
    method: 'put',
    data: data
  })
}

// 删除业务订单信息
export function delBusinessOrder(orderId) {
  return request({
    url: '/ruoyi/businessOrder/' + orderId,
    method: 'delete'
  })
}

// ============== 支付相关接口调用 ================== // 

// 获取微信支付的预交易订单号信息
export function miniAppGetPrePayId(orderId) {
	return request({
		url: '/ruoyi/businessOrder/jsapi/' + orderId,
		method: 'post',
	})
}

// 获取支付参数
export function getWxpayParams(params) {
  return request({
    url: '/ruoyi/businessOrder/getWxpayParams',
    method: 'post',
    data: {
      orderId: params.orderId,
      orderNo: params.orderNo,
      totalFee: params.totalFee,
      openId: params.openId
    }
  })
}

// 关闭订单
export function miniAppCloseOrder(orderId) {
  return request({
    url: '/ruoyi/businessOrder/miniAppCloseOrder/' + orderId,
    method: 'post'
  })
}


// 退款申请接口调用
export function miniAppRefund(orderId, reason) {
  // 参数校验
  if (!orderId || (typeof orderId !== 'string' && typeof orderId !== 'number')) {
    throw new Error('orderId 必须为字符串或数字类型');
  }
  
  if (!reason || typeof reason !== 'string') {
    throw new Error('reason 必须为非空字符串');
  }

  const encodedReason = encodeURIComponent(reason);
  return request({
    url: `/ruoyi/businessOrder/miniAppRefund/${orderId}/${encodedReason}`,
    method: 'post'
  });
}


